/*
	Copyright (C) 2009 jungil Han <jungil.Han@gmail.com>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "mkMainDialog.h"
#include "Factory/mkDialogFactory.h"
#include "../Util/mkUtil.h"

#include <QImage>
#include <QTimer>
#include <QWheelEvent>
#include <QApplication>
#include <QTextCodec>
#include <QProcess>
#include <QLabel>
#include <QWidget>
#include <QButtonGroup>
#include <QMenu>
//#include <QMessageBox>

#include <qt_windows.h>

mkMainDialog::mkMainDialog( IN QWidget *a_pParent, IN Qt::WindowFlags a_eFlags ) : mkDialog( a_pParent, a_eFlags )
{
	m_pTransparentWindow = NULL;
	m_pAboutDialog = NULL;
	m_pKeyboardDialog = NULL;
	m_pURLDialog = NULL;

	m_pHotkeyButtonGroup = NULL;
	m_pLeftButton = NULL;
	m_pLeftExtButton = NULL;
	m_pRightButton = NULL;
	m_pRightExtButton = NULL;

	m_pTrayIcon = NULL;
	m_pTrayIconMenu = NULL;
	m_pAboutAction = NULL;
	m_pEnableAutoRunAction = NULL;
	m_pDisableAutoRunAction = NULL;
	m_pEnableAction = NULL;
	m_pDisableAction = NULL;
	m_pQuitAction = NULL;

	m_nClickedButtonID = -1;
	m_nSlidingCount = 1;
	m_nSlidingDirection = eSlidingRight;
	m_pSlidingTimer = NULL;

	m_bIsSliding = false;
	m_bIsHotkeyAdd = false;
	m_bIsAligned = false;
	m_bIsEnableHotkey = true;
	m_nPosX = 0;
	m_nPosY = 0;
}

mkMainDialog::~mkMainDialog()
{
	QList<QAbstractButton *> buttonList = m_pHotkeyButtonGroup->buttons();

	 while (!buttonList.isEmpty())
		delete buttonList.takeFirst();

	MK_DELETE( m_pHotkeyButtonGroup );

	MK_DELETE( m_pLeftButton );
	MK_DELETE( m_pLeftExtButton );
	MK_DELETE( m_pRightButton );
	MK_DELETE( m_pRightExtButton );

	MK_DELETE( m_pTransparentWindow );
	MK_DELETE( m_pAboutDialog );

	MK_DELETE( m_pKeyboardDialog );
	MK_DELETE( m_pSlidingTimer );
	
	MK_DELETE( m_pURLDialog );
	
	MK_DELETE( m_pAboutAction );
	MK_DELETE( m_pEnableAutoRunAction );
	MK_DELETE( m_pDisableAutoRunAction );
	MK_DELETE( m_pEnableAction );
	MK_DELETE( m_pDisableAction );
	MK_DELETE( m_pQuitAction );
	MK_DELETE( m_pTrayIconMenu );
	MK_DELETE( m_pTrayIcon );
}

void mkMainDialog::OnInitDialog()
{
	/* Set the dialog's title */
	setWindowTitle( QString( "Monkey Hotkey " + QString( MONKEY_VERSION ) ));

	/* Set the dialog's size */
	resize( 393, 71 );
	setMaximumSize( 393, 71 );
	setMinimumSize( 393, 71 );

	/* Create the About dialog */
	mkDialogFactory dialogFactory;
	QObject *pObject = dialogFactory.CreateObject( eAboutDialog, this );
	m_pAboutDialog = static_cast<mkAboutDialog*>( pObject );

	/* Create the Keyboard dialog */
	pObject = dialogFactory.CreateObject( eKeyboardDialog, this );
	m_pKeyboardDialog = static_cast<mkKeyboardDialog*>( pObject );

	/* Create the URL dialog */
	pObject = dialogFactory.CreateObject( eURLDialog, this );
	m_pURLDialog = static_cast<mkURLDialog*>( pObject );

	/* Create the timer for Sliding */
	m_pSlidingTimer = new QTimer( this );
	connect( m_pSlidingTimer, SIGNAL( timeout() ), this, SLOT( SlotSlidingEffect() ));

	/* Create the QAction */
	m_pAboutAction = new QAction( tr("&About Monkey Hotkey"), this );
	connect(m_pAboutAction, SIGNAL( triggered() ), this, SLOT( SlotShowAbout() ));

	m_pEnableAutoRunAction = new QAction( tr("Enable Auto-&Run When Startup"), this );
	connect(m_pEnableAutoRunAction, SIGNAL( triggered() ), this, SLOT( SlotEnableAutoRun() ));

	m_pDisableAutoRunAction = new QAction( tr("Disable Auto-&Run When Startup"), this );
	connect(m_pDisableAutoRunAction, SIGNAL( triggered() ), this, SLOT( SlotDisableAutoRun() ));

	// Check the autorun option.
	if ( mkUtil::IsEnabledAutoRun() ) {
		m_pEnableAutoRunAction->setVisible( false );
	} else {
		m_pDisableAutoRunAction->setVisible( false );
	}

	m_pEnableAction = new QAction( tr("&Enable Hotkey"), this );
	m_pEnableAction->setVisible( false );
	connect(m_pEnableAction, SIGNAL( triggered() ), this, SLOT( SlotEnableHotkey() ));
	
	m_pDisableAction = new QAction( tr("&Disable Hotkey"), this );
	connect(m_pDisableAction, SIGNAL( triggered() ), this, SLOT( SlotDisableHotkey() ));
	
	m_pQuitAction = new QAction( tr("E&xit"), this );
	connect(m_pQuitAction, SIGNAL( triggered() ), qApp, SLOT( quit() ));

	/* Create the tray icon menu */
	m_pTrayIconMenu = new QMenu( this );
	m_pTrayIconMenu->addAction( m_pAboutAction );
	m_pTrayIconMenu->addSeparator();
	m_pTrayIconMenu->addAction( m_pEnableAutoRunAction );
	m_pTrayIconMenu->addAction( m_pDisableAutoRunAction );
	m_pTrayIconMenu->addAction( m_pEnableAction );
	m_pTrayIconMenu->addAction( m_pDisableAction );
	m_pTrayIconMenu->addAction( m_pQuitAction );
	
	/* Create the system tray icon */	
	m_pTrayIcon = new QSystemTrayIcon( QIcon( QString( ":/Dialog/MonkeyHotkey-32x32.png" )) , this );
	m_pTrayIcon->setVisible( true );
	m_pTrayIcon->setToolTip( windowTitle() );
	m_pTrayIcon->setContextMenu( m_pTrayIconMenu );
	connect( m_pTrayIcon, SIGNAL( activated( QSystemTrayIcon::ActivationReason )), 
            this, SLOT( SlotTrayIconActivated( QSystemTrayIcon::ActivationReason )));
	
	/* Set the ezHotkeyManager's parent */
	mkHotkeyManager::Instance()->SetParent( this );

	/* Load hotkey info */
	mkHotkeyManager::Instance()->LoadfromFile();
	QList<mkHotkeyInfo> hotkeyList = mkHotkeyManager::Instance()->GetList();
	int nMaxButtonID = 0;
	if ( hotkeyList.size() > 0 )
		nMaxButtonID = hotkeyList.last().nButtonID;

	if ( nMaxButtonID < 5 )
		nMaxButtonID = 5;

	/* Create the transparent window */
	m_pTransparentWindow = new QWidget( this );
	m_pTransparentWindow->setAttribute(Qt::WA_TranslucentBackground);
	m_pTransparentWindow->setGeometry( 22, 1, (( 70 ) * nMaxButtonID) , 69 );

	/* Create the Hotkey button group */
	m_pHotkeyButtonGroup = new QButtonGroup( this );
	
	/* Create the Hotkey button */
	for ( int nCnt = 0; nCnt < nMaxButtonID; ++nCnt) {
		mkButton *pButton = new mkHotkeyButton( m_pTransparentWindow, nCnt + 1 );
		pButton->SetBGImage( QString( ":/Button/main_btn_bg_default.png" ), QString( ":/Button/main_btn_bg_over.png" ), QString( ":/Button/main_btn_bg_click.png" ));
		pButton->setGeometry( 0 + ( 70 * nCnt ), 0, 69, 69 );
		m_pHotkeyButtonGroup->addButton( pButton, nCnt + 1 );
	}

	LoadHotkeyInfo();

	/* Create the arrow button */
	QRect arrowIconRect( 2, 17, 16, 16 );
	m_pLeftButton = new mkButton( this );
	m_pLeftButton->SetBGImage( QString( ":/Button/arrow_btn_bg_default.png" ), QString( ":/Button/arrow_btn_bg_over.png" ), QString( ":/Button/arrow_btn_bg_over.png" ));
	m_pLeftButton->SetFGImage( QString( ":/Button/left_arrow_icon_default.png" ), QString( ":/Button/left_arrow_icon_over.png" ), QString(  ":/Button/left_arrow_icon_click.png" ), &arrowIconRect );
	m_pLeftButton->setGeometry( 1, 1, 20, 49 );
	m_pLeftButton->setAutoRepeat ( true );
	m_pLeftButton->setAutoRepeatInterval ( 50 );
	connect( m_pLeftButton, SIGNAL( clicked() ), this, SLOT( SlotLeftClicked() ));
	
	m_pRightButton = new mkButton( this );
	m_pRightButton->SetBGImage( QString( ":/Button/arrow_btn_bg_default.png" ), QString( ":/Button/arrow_btn_bg_over.png" ), QString( ":/Button/arrow_btn_bg_over.png" ));	
	m_pRightButton->SetFGImage( QString( ":/Button/right_arrow_icon_default.png" ), QString( ":/Button/right_arrow_icon_over.png" ), QString( ":/Button/right_arrow_icon_click.png" ), &arrowIconRect );
	m_pRightButton->setGeometry( 22 + 350, 1, 20, 49 );
	m_pRightButton->setAutoRepeat ( true );
	m_pRightButton->setAutoRepeatInterval ( 50 );	
	connect( m_pRightButton, SIGNAL( clicked() ), this, SLOT( SlotRightClicked() ));
	
	/* Create the extension button */
	QRect extIconRect( 4, 3, 14, 14 );
	m_pLeftExtButton = new mkButton( this );
	m_pLeftExtButton->SetBGImage( QString( ":/Button/extension_btn_bg_default.png" ), QString( ":/Button/extension_btn_bg_over.png" ), QString( ":/Button/extension_btn_bg_over.png" ));	
	m_pLeftExtButton->SetFGImage( QString( ":/Button/refresh_icon_default.png" ), QString( ":/Button/refresh_icon_over.png" ), QString( ":/Button/refresh_icon_click.png" ), &extIconRect );
	m_pLeftExtButton->setGeometry( 1, 51, 20, 19 );
	m_pLeftExtButton->setToolTip( QString( "Align" ));
	connect( m_pLeftExtButton, SIGNAL( clicked() ), this, SLOT( SlotAlignClicked() ));	
	
	m_pRightExtButton = new mkButton( this );
	m_pRightExtButton->SetBGImage( QString( ":/Button/extension_btn_bg_default.png" ), QString( ":/Button/extension_btn_bg_over.png" ), QString( ":/Button/extension_btn_bg_over.png" ));
	m_pRightExtButton->SetFGImage( QString( ":/Button/plus_icon_default.png" ), QString( ":/Button/plus_icon_over.png" ), QString( ":/Button/plus_icon_click.png"), &extIconRect );
	m_pRightExtButton->setGeometry( 22 + 350, 51, 20, 19 );
	m_pRightExtButton->setToolTip( QString( "Add" ));
	connect( m_pRightExtButton, SIGNAL( clicked() ), this, SLOT( SlotAddClicked() ));

	/* Create Decoration Label */
	QPixmap decoPixmap( 1, 71 );
	decoPixmap.fill( Qt::black );

	QLabel *pDecoLeftLabel = new QLabel( this );
	pDecoLeftLabel->setPixmap( decoPixmap );
	pDecoLeftLabel->setGeometry( 0, 0, 1, 71 );

	QLabel *pDecoRightLabel = new QLabel( this );
	pDecoRightLabel->setPixmap( decoPixmap );
	pDecoRightLabel->setGeometry( 392, 0, 1, 71 );
}

bool mkMainDialog::IsEnableHotkey()
{
	return m_bIsEnableHotkey;
}

void mkMainDialog::LoadHotkeyInfo()
{
	QList<mkHotkeyInfo> hotkeyList = mkHotkeyManager::Instance()->GetList();
	
	for ( int nIndex = 0; nIndex < hotkeyList.size(); nIndex++ ) {
		mkHotkeyButton *pButton = static_cast<mkHotkeyButton *>(m_pHotkeyButtonGroup->button( hotkeyList.at( nIndex ).nButtonID ));
		if ( pButton ) {
			mkHotkeyInfo hotkeyInfo = hotkeyList.at( nIndex );
			pButton->LoadHotkey( &hotkeyInfo );
		}
	}
}

void mkMainDialog::AddHotkeyButton()
{
	QSize size = m_pTransparentWindow->size();
	size.rwidth() += 70;
	m_pTransparentWindow->resize( size );
	m_pTransparentWindow->setAttribute( Qt::WA_TranslucentBackground );
	
	QList<QAbstractButton *> buttonList = m_pHotkeyButtonGroup->buttons();
	int nButtonCount = buttonList.size();

	mkButton *pButton = new mkHotkeyButton( m_pTransparentWindow, nButtonCount + 1 );
	pButton->SetBGImage( QString( ":/Button/main_btn_bg_default.png" ), QString( ":/Button/main_btn_bg_over.png" ), QString( ":/Button/main_btn_bg_click.png" ));
	pButton->setGeometry( 0 + ( 70 * nButtonCount ), 0, 69, 69 );
	pButton->show();
	m_pHotkeyButtonGroup->addButton( pButton, nButtonCount + 1 );
}

void mkMainDialog::RemoveEmptyButton()
{
	QList<QAbstractButton *> buttonList = m_pHotkeyButtonGroup->buttons();
	QList<mkHotkeyInfo> hotkeyList = mkHotkeyManager::Instance()->GetList();

	int nButtonListSize = buttonList.size();
	int nHotkeyListSize = hotkeyList.size();

	if ( nButtonListSize > nHotkeyListSize && nButtonListSize > 5) {
		for ( int nIndex = nHotkeyListSize + 1; nIndex < nButtonListSize + 1; nIndex++) {
			delete buttonList.takeLast();

			/* Resize the transparent window */
			QSize size = m_pTransparentWindow->size();
			size.rwidth() -= 70;
			m_pTransparentWindow->resize( size );
			m_pTransparentWindow->setAttribute( Qt::WA_TranslucentBackground );

			if ( buttonList.size() == 5 )
				break;
		}
	}
}

void mkMainDialog::UpdateTrayIconToolTip()
{
	QString strToolTip( QString( windowTitle() ) + QString( "\n\n" ));

	QList<mkHotkeyInfo> hotkeyList = mkHotkeyManager::Instance()->GetList();
	
	for ( int nIndex = 0; nIndex < hotkeyList.size(); nIndex++ ) {
		mkHotkeyButton *pButton = static_cast<mkHotkeyButton *>(m_pHotkeyButtonGroup->button( hotkeyList.at( nIndex ).nButtonID ));
		if ( pButton ) {
			int nSystemKey, pNonSystemKey;
			pButton->GetHotkeyCode( &nSystemKey, &pNonSystemKey );

			QString strSystemKey, strNonSystemKey;
			mkUtil::TranslateKeyEnumToText( nSystemKey, pNonSystemKey, &strSystemKey, &strNonSystemKey );
				
			strToolTip += QString( "[" + strSystemKey + "+" + strNonSystemKey + "] ") + QString( pButton->GetName().toUpper() + "\n");
		}
	}
		
	m_pTrayIcon->setToolTip( strToolTip );
}

void mkMainDialog::hideEvent( IN QHideEvent *a_pEvent )
{
	//setWindowTitle( QString( "HideEvent: %1 x %2").arg( m_nPosX ).arg( m_nPosY ));

	m_nPosX = pos().x();
	m_nPosY = pos().y();

	a_pEvent->ignore();
}

void mkMainDialog::wheelEvent( QWheelEvent* a_pEvent )
{
	if ( m_bIsSliding )
		return ;
	
	int nNumDegrees = a_pEvent->delta() / 8;
	int nNumSteps = nNumDegrees / 15;

	//setWindowTitle( QString( "%1 %2").arg( nNumSteps ).arg( nNumDegrees ));
	
	if ( nNumSteps > 0 )
	        SlotLeftClicked();
	else
	        SlotRightClicked();
}

void mkMainDialog::closeEvent( QCloseEvent *a_pEvent )
{
	// showMinimized를 명시적으로 호출하지 않을 경우, 메모리 사용량이 줄어들지 않음
	showMinimized();
	//hide();
}	

void mkMainDialog::keyPressEvent( QKeyEvent *a_pEvent )
{
	switch ( a_pEvent->key() ) {
		case Qt::Key_F1:
			if ( a_pEvent->modifiers() == Qt::NoModifier ) {
				m_pAboutDialog->exec();
				a_pEvent->accept();
			} else {
				a_pEvent->ignore();
			}
			break;
		case Qt::Key_Escape:
			showMinimized();
			//hide();
			a_pEvent->accept();
		default:
			a_pEvent->ignore();			
			break;
	}
}

bool mkMainDialog::winEvent( MSG *a_pMessage, long *a_pResult )
{
	switch ( a_pMessage->message ) {
		case WM_HOTKEY:
		{
			QList<mkHotkeyInfo> hotkeyList = mkHotkeyManager::Instance()->GetList();
			QList<mkHotkeyInfo>::iterator it;

			for ( it = hotkeyList.begin(); it < hotkeyList.end(); it++ ) {
				if ( a_pMessage->wParam == (unsigned int)it->nButtonID ) {
					Execute( it->strFileName );
					return true;
				}
			}

		} break;
		case WM_APP + 1:
		{
			if ( isHidden() )
				move( m_nPosX, m_nPosY );

			if ( isHidden() || isMinimized() )
					showNormal();

			activateWindow();
			return true;
		}
	}

    return false;
}

void mkMainDialog::Execute( IN QString &a_refFileName )
{
	QString strOptioin = "open";
	wchar_t szOption[32] = {0, };
	strOptioin.toWCharArray( szOption );

	QString strAppFullPath = "\"" + a_refFileName + "\"";
	wchar_t szAppFullPath[256] = {0, };
	strAppFullPath.toWCharArray( szAppFullPath );

	ShellExecute(NULL, szOption, szAppFullPath, NULL, NULL, SW_SHOWNORMAL );
}

/*
 * Hotkey 설정시 수정할 버튼을 찾기위해 ButtonID 필요.
 */
void mkMainDialog::SlotKeyboardShow( int a_nHotkeyButtonID )
{
	m_nClickedButtonID = a_nHotkeyButtonID;
	mkHotkeyButton *pButton = static_cast<mkHotkeyButton *>(m_pHotkeyButtonGroup->button( m_nClickedButtonID ));

	int nSystemKey, nNonSystemKey;
	pButton->GetHotkeyCode( &nSystemKey, &nNonSystemKey );

	QString strSystemKeyText, strNonSystemKeyText;
	mkUtil::TranslateKeyEnumToText( nSystemKey, nNonSystemKey, &strSystemKeyText, &strNonSystemKeyText );
	
	m_pKeyboardDialog->SetToggle( strSystemKeyText );
	m_pKeyboardDialog->SetToggle( strNonSystemKeyText );

	//QMessageBox::information( this, QString( "Debug" ), QString( "[%1] " ).arg( m_nClickedButtonID ) + QString( strSystemKeyText + " + " + strNonSystemKeyText ));

	m_pKeyboardDialog->exec();
}

void mkMainDialog::SlotHotkeyInfoUpdated( QString a_strSystemKey, QString a_strNonSystemKey )
{	
	int nSystemKey, nNonSystemKey;
	mkUtil::TranslateKeyTextToEnum( a_strSystemKey, a_strNonSystemKey, &nSystemKey, & nNonSystemKey );

	mkHotkeyButton *pButton = static_cast<mkHotkeyButton *>(m_pHotkeyButtonGroup->button( m_nClickedButtonID ));

	mkHotkeyManager::Instance()->AddHotkey( pButton->GetIndex(), nSystemKey, nNonSystemKey, pButton->GetFilePath() );
	pButton->RegisterHotkey( nSystemKey, nNonSystemKey );
	
	pButton->EnableIcon( true );
	pButton->update();

	//UpdateTrayIconToolTip();
	//QMessageBox::information( this, QString(" Debug" ), QString( "[%1] [%2] %3 [%4] %5 %6" ).arg( pButton->GetIndex() ).arg( a_strSystemKey ).arg( nSystemKey ).arg( a_strNonSystemKey ).arg( nNonSystemKey ).arg( pButton->GetFilePath() ));
}

void mkMainDialog::SlotLeftClicked()
{
	if ( m_bIsSliding )
		return ;

	if ( m_pTransparentWindow->pos().x() < 22 ) {
		if ( m_bIsAligned ) {
			m_nSlidingCount = ( qAbs( m_pTransparentWindow->pos().x() ) + 22 ) / 70;
		} else {
			m_nSlidingCount = 1;
		}
		
		m_nSlidingDirection = eSlidingLeft;
		m_pSlidingTimer->start( 10 );
	}
}

void mkMainDialog::SlotRightClicked()
{
	if ( m_bIsSliding )
		return ;

	if ( m_pTransparentWindow->pos().x() +  m_pTransparentWindow->width() > (22 + 5 * 70) ) {
		if ( m_bIsHotkeyAdd ) {
			m_nSlidingCount = ( m_pTransparentWindow->pos().x() + m_pTransparentWindow->width() - ( 22 + 5 * 70 ))  / 70;
		} else {
			m_nSlidingCount = 1;	
		}
		
		m_nSlidingDirection = eSlidingRight;
		m_pSlidingTimer->start( 10 );
	}
}

void mkMainDialog::SlotAlignClicked()
{
	QList<QAbstractButton *> buttonList = m_pHotkeyButtonGroup->buttons();
	
	for ( int nIndex = 0; nIndex < buttonList.size(); nIndex++) {
		QString strFilePath = static_cast<mkHotkeyButton *>(buttonList.at( nIndex ))->GetFilePath();
		if ( !strFilePath.isEmpty() ) {
			UnregisterHotKey( winId(), static_cast<mkHotkeyButton *>(buttonList.at( nIndex ))->GetIndex() );
		}
	}

	mkHotkeyManager::Instance()->SortByHotkey();
	LoadHotkeyInfo();

	QList<mkHotkeyInfo> hotkeyList = mkHotkeyManager::Instance()->GetList();
	
	for ( int nIndex = hotkeyList.size() + 1; nIndex < buttonList.size() + 1; nIndex++ ) {
		mkHotkeyButton *pButton = static_cast<mkHotkeyButton *>(m_pHotkeyButtonGroup->button( nIndex ));
		pButton->SlotDeleteClicked();
	}

	// Sliding to the left when x position of the transparentWindow is less then 22
	if ( m_pTransparentWindow->pos().x() < 22 ) {
		/* Sliding to the first position */
		m_bIsAligned = true;
		SlotLeftClicked();
	} else {
		RemoveEmptyButton();
	}

	//QMessageBox::information( this, QString( "Debug" ), QString( "[%1] [%2]" ).arg( buttonList.size() ).arg( m_pTransparentWindow->size().width() ));	
	//setWindowTitle( QString( "[%1] [%2]" ).arg( buttonList.size() ).arg( m_pTransparentWindow->size().width() ));		
}

void mkMainDialog::SlotAddClicked()
{
	m_bIsHotkeyAdd = true;
	
	AddHotkeyButton();
	SlotRightClicked();
}

void mkMainDialog::SlotShowAbout()
{
	m_pAboutDialog->exec();
}

void mkMainDialog::SlotEnableAutoRun()
{
	mkUtil::RegisterAutoRun( true );

	m_pEnableAutoRunAction->setVisible( false );
	m_pDisableAutoRunAction->setVisible( true );
}

void mkMainDialog::SlotDisableAutoRun()
{
	mkUtil::RegisterAutoRun( false );

	m_pEnableAutoRunAction->setVisible( true );
	m_pDisableAutoRunAction->setVisible( false );
}

void mkMainDialog::SlotEnableHotkey()
{
	m_bIsEnableHotkey = true;
	mkHotkeyManager::Instance()->RegisterHotkeyAll();

	m_pEnableAction->setVisible( false );
	m_pDisableAction->setVisible( true );		
}

void mkMainDialog::SlotDisableHotkey()
{
	m_bIsEnableHotkey = false;
	mkHotkeyManager::Instance()->UnregisterHotkeyAll();
	
	m_pEnableAction->setVisible( true );
	m_pDisableAction->setVisible( false );	
}

void mkMainDialog::SlotQuit()
{
	mkHotkeyManager::Instance()->SavetoFile();
	mkHotkeyManager::Instance()->RemoveHotkeyAll();
}

void mkMainDialog::SlotSlidingEffect()
{
	static int nPrevPosX = m_pTransparentWindow->pos().x();
	static int nStep = 10;
	m_bIsSliding = true;
		
	if ( m_nSlidingDirection == eSlidingLeft ) {
		QPoint point( m_pTransparentWindow->pos().x(), m_pTransparentWindow->pos().y() );
		m_pTransparentWindow->move( point.rx() += nStep,  1);
	} else if ( m_nSlidingDirection == eSlidingRight ) {
		QPoint point( m_pTransparentWindow->pos().x(), m_pTransparentWindow->pos().y() );
		m_pTransparentWindow->move( point.rx() -= nStep,  1);
	}

	//setWindowTitle( QString( "PrevX:%1 X: %2 Count: %3" ).arg( nPrevPosX ).arg( m_pTransparentWindow->pos().x()).arg( m_nSlidingCount ));
	
	/* [Bug fix] Align 버튼 클릭 후, Add 버튼 클릭 할 경우, -49를 체크하지 못하는 에러로 예외 추가 */
	 if ( qAbs( nPrevPosX - m_pTransparentWindow->pos().x() ) == 60 *  m_nSlidingCount ) {
		nStep = 5;
	} else if ( qAbs( nPrevPosX - m_pTransparentWindow->pos().x() ) == 70 * m_nSlidingCount 
		|| (m_nSlidingDirection == eSlidingRight && m_pTransparentWindow->pos().x() +  m_pTransparentWindow->width() <= (22 + 5 * 70) )) {
		m_pSlidingTimer->stop();
		nPrevPosX = m_pTransparentWindow->pos().x();
		nStep = 10;

		if ( m_bIsAligned )
			RemoveEmptyButton();

		m_bIsHotkeyAdd = false;
		m_bIsAligned = false;
		m_bIsSliding = false;
	}
}

void mkMainDialog::SlotTrayIconActivated( QSystemTrayIcon::ActivationReason a_eReason )
{
	switch ( a_eReason ) {
	case QSystemTrayIcon::DoubleClick:
		if ( isHidden() )
			move( m_nPosX, m_nPosY );
		if ( isHidden() || isMinimized() )
			showNormal();
			
			activateWindow();
		break;
	default:
		break;
	}
}

QString mkMainDialog::ShowURLDialog( IN const QString &a_refCurrentUrl )
{
	QString strURL("");

	if ( m_pURLDialog->Show( a_refCurrentUrl )) {
		strURL = m_pURLDialog->GetText();
	}

	return strURL;
}
